package com.usefullc.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.domain.AppDevice;
import com.usefullc.system.mapper.AppDeviceMapper;
import com.usefullc.system.query.AppDeviceQuery;
import com.usefullc.system.query.BaseQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class AppDeviceService extends AbstractBizService<AppDeviceMapper,AppDevice,AppDeviceQuery> {

    @Autowired
    private AppDeviceMapper appDeviceMapper;


    @Override
    public QueryWrapper<AppDevice> getQueryWrapper(AppDeviceQuery query) {
        QueryWrapper<AppDevice> queryWrapper = super.getQueryWrapper(query);
        return queryWrapper;
    }


    public AppDevice getDevice(String deviceId,String platform,String appName){
        AppDeviceQuery query = new AppDeviceQuery();
        query.setDeviceId(deviceId);
        query.setPlatform(platform);
        query.setAppName(appName);
//        query.setAppVersion(appVersion);
        AppDevice appDevice = this.findOne(query);
        return appDevice;

    }

    public AppDevice report(AppDevice appDevice){
        AppDevice dbAppDevice = getDevice(appDevice.getDeviceId(), appDevice.getPlatform(), appDevice.getAppName());
        if(dbAppDevice != null){
            //修改相关数据
            BeanUtils.beanCopy(appDevice,dbAppDevice);
            appDevice = dbAppDevice;
        }else{
            //app 第一次上报，生成数据保存
            String deviceId = UUID.randomUUID().toString();  //暂时用这个，重复几率几乎为0，后续再添加防重复因子
            appDevice.setDeviceId(deviceId);

        }
        this.save(appDevice);
        return appDevice;


    }


}
